﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Theming;
using ProModel.ApplicationFramework.Silverlight;
using System.Windows.Resources;
using System.Windows.Media.Imaging;

namespace ProModelWindows.Silverlight.AST.Demo
{
	public partial class MainDemoPage : UserControl
    {
        #region Fields and Properties

        BitmapImage _titleImage;

        #endregion

        #region Construction

        public MainDemoPage()
        {
	        // Required to initialize variables
	        InitializeComponent();
        }

        #endregion

        #region Theme

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            StreamResourceInfo sr = Application.GetResourceStream(new Uri("ProModelWindows.Silverlight.AST.Demo;component/Resources/Exclaimation.png", UriKind.Relative));
            _titleImage = new BitmapImage();
            _titleImage.SetSource(sr.Stream);

            string themeToLoad = @"Themes/Microsoft.Windows.Controls.Theming.ExpressionDark.xaml";
            SetTheme(LayoutRoot as FrameworkElement, themeToLoad);
        }

        public static void SetTheme(FrameworkElement control, string theme)
        {
            Uri uri = new Uri(@"ProModelWindows.Silverlight.AST.Demo;component/" + theme, UriKind.Relative);
            ImplicitStyleManager.SetResourceDictionaryUri(control, uri);
            ImplicitStyleManager.SetApplyMode(control, ImplicitStylesApplyMode.Auto);
            ImplicitStyleManager.Apply(control);
        }

        #endregion

        #region Modal Handling

        private void _btnShowWindow_Click(object sender, RoutedEventArgs e)
        {
            if (_tcTabs.SelectedItem == _tpWndResize)
            {
                ShowWindowedModal(OnDialogClosed, LayoutRoot);
            }
            else if (_tcTabs.SelectedItem == _tpWndNoResize)
            {
                ShowWindowedModalNoResize(OnDialogClosed, LayoutRoot);
            }
            else
            {
                ShowModal(OnDialogClosed, LayoutRoot);
            }
        }

        public void ShowModal(DialogClose callback, Panel panel)
        {
            bool useCanvasPainting = true;
            ModalControl wmc = new ModalControl(new DialogClose(OnDialogClosed));
            TestUserControl tc = new TestUserControl();
            tc._tblMessage.Text = "A non-windowed modal, renders the UserControl as is (ESC to exit)";
            wmc.HostedControl = tc;
            wmc.ShowModal(panel, useCanvasPainting, new Size(tc.MinWidth, tc.MinHeight));
        }

        public void ShowWindowedModal(DialogClose callback, Panel panel)
        {
            bool useCanvasPainting = true;
            WindowedModalControl wmc = new WindowedModalControl(callback, MsgBoxButtons.OKCancelApply);
            TestUserControl tc = new TestUserControl();
            wmc.TitleBarBrush = Resources["ModalHeaderBrush1"] as Brush;
            wmc.FormBackgroundBrush = Resources["ModalBrush1"] as Brush;
            wmc.TitleImage = _titleImage;
            wmc.HostedControl = tc;
            wmc.ShowModal(panel, useCanvasPainting);
            wmc.AllowResizing = true;
            wmc.AllowMaximize = true;
            wmc.TitleBarText = "It's a Working Title...and it truncates if need be";
        }

        public void ShowWindowedModalNoResize(DialogClose callback, Panel panel)
        {
            bool useCanvasPainting = true;
            WindowedModalControl wmc = new WindowedModalControl(callback, MsgBoxButtons.OKCancel);
            TestUserControl tc = new TestUserControl();
            tc._tblMessage.Text = "A windowed modal that cannot be resized";
            wmc.TitleBarBrush = Resources["ModalHeaderBrush2"] as Brush;
            wmc.FormBackgroundBrush = Resources["ModalBrush2"] as Brush;
            wmc.TitleImage = _titleImage;
            wmc.HostedControl = tc;
            wmc.ShowModal(panel, useCanvasPainting, new Size(tc.MinWidth, tc.MinHeight));
            wmc.AllowResizing = false;
            wmc.AllowMaximize = false;
            wmc.TitleBarText = "This can't be resized";
        }

        bool OnDialogClosed(object sender, DialogExit e)
        {
            _tbResult.Text = "Dialog Result: " + e.ToString();
            return true;
        }

        #endregion
    }
}